package stu.aistar.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import stu.aistar.mapper.MovieTypeMapper;
import stu.aistar.model.pojo.MovieAllTypes;
import stu.aistar.model.pojo.MovieType;
import stu.aistar.service.MovieTypeService;

import java.util.List;
@Service
public class MovieTypeServiceImpl implements MovieTypeService {

    @Autowired
    private MovieTypeMapper movieAllTypesMapper;

    @Autowired
    private MovieTypeMapper movieTypeMapper;

    @Override
    public List<MovieType> findAll() {
        return movieTypeMapper.findAll();
    }

    @Override
    public PageInfo<MovieType> findByPage(Integer startPage, Integer pageSize) {
        PageHelper.startPage(startPage,pageSize);
        List<MovieType> types = movieTypeMapper.findAll();
        PageInfo<MovieType> pageInfo = new PageInfo<>(types);
        return pageInfo;
    }

    @Override
    public Integer findTotal() {
        return movieTypeMapper.findTotal();
    }

    @Override
    public MovieType findMovieTypeByName(String name) {
        return movieTypeMapper.findMovieTypeByName(name);
    }

    @Override
    public Integer insertMovieType(String name) {
        return movieTypeMapper.insertMovieType(name);
    }

    @Override
    public Boolean updateTypeById(MovieType movieType) {
        Integer row = movieTypeMapper.updateTypeById(movieType);
        return row==1?true:false;
    }

    @Override
    public Boolean deleteTypeByName(String name) {
        Integer row = movieTypeMapper.deleteTypeByName(name);
        return row==1?true:false;
    }

    /**
     * 根据id查询该电影的所有电影类型
     * @param id
     * @return
     */
    @Override
    public List<MovieType> findMovieTypes(Integer id) {
        MovieAllTypes movieAllTypes = movieAllTypesMapper.findMovieTypes(id);
        //电影类型id
        String[] types= movieAllTypes.getTypeIds().split(",");
        return movieTypeMapper.findMovieTypeByIds(types);
    }

    /**
     * 存入电影id的电影类型
     * @param typeIds
     * @param movieId
     * @return
     */
    @Override
    public Integer saveMovieTypes(String typeIds, Integer movieId) {
        return movieAllTypesMapper.insertMovieTypes(typeIds,movieId);
    }
}
